package com.ruoyi.web.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.H5User;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.mapper.UserShopMapper;
import com.ruoyi.web.domain.UserShop;
import com.ruoyi.web.service.IUserShopService;

/**
 * 商品Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-06-11
 */
@Service
public class UserShopServiceImpl implements IUserShopService 
{
    @Autowired
    private UserShopMapper userShopMapper;

    /**
     * 查询商品
     * 
     * @param id 商品主键
     * @return 商品
     */
    @Override
    public UserShop selectUserShopById(Long id)
    {
        return userShopMapper.selectUserShopById(id);
    }

    /**
     * 查询商品列表
     * 
     * @param userShop 商品
     * @return 商品
     */
    @Override
    public List<UserShop> selectUserShopList(UserShop userShop)
    {

        LoginUser loginUser = SecurityUtils.getLoginUser();
        String client =SecurityUtils.getClientType();
        if(!client.equals("h5")){
            SysUser sysUser = loginUser.getUser();
            if(sysUser.getRoles().get(0).getRoleId() != 1){
                userShop.setUserid(Long.parseLong(sysUser.getBindUserId()));
            }
        }else{
            H5User h5User = loginUser.getH5User();
            if(h5User.getTop().equals(0L)){
                userShop.setUserid(h5User.getId());
            }else{
                userShop.setUserid(h5User.getTop());
            }
        }
        
        return userShopMapper.selectUserShopList(userShop);
    }

    /**
     * 新增商品
     * 
     * @param userShop 商品
     * @return 结果
     */
    @Override
    public int insertUserShop(UserShop userShop)
    {
        userShop.setCreateTime(DateUtils.getNowDate());
        return userShopMapper.insertUserShop(userShop);
    }

    /**
     * 修改商品
     * 
     * @param userShop 商品
     * @return 结果
     */
    @Override
    public int updateUserShop(UserShop userShop)
    {
        userShop.setUpdateTime(DateUtils.getNowDate());
        return userShopMapper.updateUserShop(userShop);
    }

    /**
     * 批量删除商品
     * 
     * @param ids 需要删除的商品主键
     * @return 结果
     */
    @Override
    public int deleteUserShopByIds(Long[] ids)
    {
        return userShopMapper.deleteUserShopByIds(ids);
    }

    /**
     * 删除商品信息
     * 
     * @param id 商品主键
     * @return 结果
     */
    @Override
    public int deleteUserShopById(Long id)
    {
        return userShopMapper.deleteUserShopById(id);
    }
}
